// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GamePadEmulatorAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Interaction.Runtime.Input
{
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Emulates a game pad by publishing its game pad message state which can be updated by control messages.
    /// </summary>
    [DataContract]
    public class GamePadEmulatorAgent : ConsumerProducerAgent<TimerMessage, GamePadAgentMessage>, IControlReceiver<GamePadAgentMessage>
    {
        /// <summary>
        /// Initializes a new instance of the GamePadEmulatorAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">Producer agent</param>
        public GamePadEmulatorAgent(string name, IProducer<TimerMessage> producer)
            : base(name, producer.Name)
        {
        }

        /// <summary>
        /// Gets or sets the state
        /// </summary>
        private GamePadAgentMessage State { get; set; }

        /// <summary>
        /// Initializes any fields not provided in the manifest.
        /// </summary>
        /// <param name="locator">Not used.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.State = new GamePadAgentMessage(
                new GamePadAgentMessage.GamePadAgentButtons(),
                new GamePadAgentMessage.GamePadAgentDPad(),
                new GamePadAgentMessage.GamePadAgentThumbSticks(),
                new GamePadAgentMessage.GamePadAgentTriggers());
        }

        /// <summary>
        /// Sets drive velocities
        /// </summary>
        /// <param name="setMessage">The message containing the SetDriveVelocities command</param>
        public void ReceiveControl(GamePadAgentMessage setMessage)
        {
            this.State = setMessage;
        }

        /// <summary>
        /// Publishes the game pad state
        /// </summary>
        /// <param name="message">An agent message triggering state update</param>
        public override void Receive(TimerMessage message)
        {
            GamePadAgentMessage newMessage = new GamePadAgentMessage(
                this.State.Buttons,
                this.State.DPad,
                this.State.ThumbSticks,
                this.State.Triggers);

            this.Publisher.Post(newMessage);
        }
    }
}
